package com.firmboy.pattern.adapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author firmboy
 * @create 2018-06-19 下午3:51
 **/
public class DispatchServlet {


    public static List<HandlerAdapter> handlerAdapters = new ArrayList<HandlerAdapter>();

    public DispatchServlet(){
        handlerAdapters.add(new AnnotationHandlerAdapter());
        handlerAdapters.add(new HttpHandlerAdapter());
        handlerAdapters.add(new SimpleHandlerAdapter());
    }

    public void doDispatch(){
        //HttpController controller = new HttpController();
        //AnnocationController controller = new AnnocationController();
        SimpleController controller = new SimpleController();
        HandlerAdapter adapter = getHandler(controller);
        adapter.handle(controller);

    }


    public HandlerAdapter getHandler(Controller controller){
        for (HandlerAdapter adapter:this.handlerAdapters){
            if(adapter.supports(controller)){
                return adapter;
            }
        }
        return null;
    }

    public static void main(String[] args){
        DispatchServlet dispatchServlet = new DispatchServlet();
        dispatchServlet.doDispatch();
    }

}
